<?php

class Patientmodel extends Model{
	
	// user object model vars
	var $search_model = array();
	var $data_model = array();	
	var $form_model = array();		
	var $view_model = array();
	var $list_model = array();
	var $object = "patient";	
    var $myform;
	
	
	function patientmodel(){
		
		parent::Model();	

		$this->load->library("form");
		$this->myform = new Form();
		$this->load->library("cdata_user");
		$this->load->library("relation");
		
		// data model
		$this->data_model = array(
		'fields' => array(
			"pclass" => array(
    			"type" => "text"
    			),
    		"pid" => array(
    			"type" => "integer"
    			),
    		"code" => array(
    			"type" => "text"
    			),
    		"dob" => array(
    			"type" => "date"
    			),
    		"note" => array(
    			"type" => "text"
    			),
    		"registered" => array(
    			"type" => "date"
    			),
    		"sex" => array(
    			"type" => "text",
    			"serialized" => "true"
    			),
    		"centre" => array(
    			"type" => "integer",
    			"serialized" => "true"
    			),
    		"refid" => array(
    			"type" => "text",
    			"serialized" => "true"
    			),
              "complete" => array(
    			"type" => "text",
    			"serialized" => "true"
    			),
              "description" => array(
    			"type" => "text",
    			"serialized" => "true"
    			),
    		"created" => array(
    			"type" => "date"
    			)
    		),
        	'table' => array(
    		'name' => "{$this->object}",
    		'id' => $this->object.'_id',
    		"serialized_column" => "serialized")
		);
		
		// view model
		$this->view_model = array(
		"fields" => array(
    		"code" => array(
    			"type" => "text",
    			"label" => "Code",
    			"tab" => "tab1"
    			),
              "description" => array(
    			"type" => "text",
    			"label" => "Description",
    			"tab" => "tab1"
    			),
		"refid" => array(
    			"type" => "text",
    			"label" => "Reference ID",
    			"tab" => "tab1"
    			),    			
    		"dob" => array(
    			"type" => "date",
    			"label" => "Date of Birth",
    			"tab" => "tab1"
    			),    		
    		"sex" => array(
    			"type" => "select",
    			"label" => "Gender",
    			"tab" => "tab1"
    			),
    		"centre" => array(
    			"type" => "select",
    			"label" => "Centre",
    			"tab" => "tab1"
    			),    		
    		"registered" => array(
    			"type" => "date",
    			"label" => "Registered",
    			"tab" => "tab1"
    			),
              "complete" => array(
    			"type" => "select",
    			"label" => "Reg. complete",
    			"tab" => "tab1"
    			), 
    		"note" => array(
    			"type" => "html",
    			"label" => "Note",
    			"tab" => "tab2"
    			)
    		),
    	"view" => array(
    		"theme" => "default",
    		"tab" => array(
				"tab1" => "Anagraphic Data",
				"tab2" => "Note"
				)
    	)
		);
		
		// form model
        $this->form_model = array(
		"fields" => array(
			"pclass" => array(
    			"type" => "hidden"
    			), 
    		"pid" => array(
    			"type" => "hidden"
    			), 
    		"code" => array(
    			"type" => "text",
    			"label" => "Code",
    			"length" => "30",
    			"required" => "true",
    			"tab" => "tab1"
    			),  
    		"description" => array(
    			"type" => "text",
    			"label" => "Description",
    			"length" => "60",
    			"required" => "false",
    			"tab" => "tab1"
    			),  
    		"refid" => array(
    			"type" => "text",
    			"label" => "Reference ID",
    			"length" => "30",
    			"required" => "false",
    			"tab" => "tab1"
    			),  
    		"dob" => array(
    			"type" => "date",
    			"label" => "Date of Birth",
    			"length" => "20",
    			"required" => "false",
    			"tab" => "tab1"
    			), 
    		"registered" => array(
    			"type" => "date",
    			"label" => "Registration Date",
    			"length" => "20",
    			"required" => "true",
    			"tab" => "tab1"
    			),
    		"sex" => array(
    			"type" => "select",
    			"label" => "Gender",
    			"lenght" => "",
    			"required" => "false",
    			"tab" => "tab1",
    			"select" => array(
    				"multiple" => "false",
    				"null_value" => "false",
    				"interface" => "radio", 
    				"source" => array(
    					"type" => "array",
    					"options" => array("m" => "Male", "f" =>"Female")
    					)
    				)
    			),
    		"centre" => array(
        			"type" => "select",
        			"label" => "Centre",
        			"tab" => "tab1",
        			"select" => array(
        				"multiple" => "false",
        				"null_value" => "true",
        				"interface" => "list",
        				"source" => array(
        					"type" => "query",
        					"table" =>"centre",
        					"id" => "centre_id",
        					"label" => "name",
        					"sortby" => "name",
        					"order" => "ASC"
        				)
    				)
    			),
              "complete" => array(
    			"type" => "select",
    			"label" => "Registration complete",
    			"lenght" => "",
    			"required" => "false",
    			"tab" => "tab1",
    			"select" => array(
    				"multiple" => "false",
    				"null_value" => "true",
    				"interface" => "list", 
    				"source" => array(
    					"type" => "array",
    					"options" => array("y" => "Yes", "n" =>"No")
    					)
    				)
    			),
    		"note" => array(
    			"type" => "textarea",
    			"label" => "Note",
    			"rows" => "30",
    			"cols" => "80",
    			"required" => "false",
    			"editor" => "true",
    			"default" =>"",
    			"tab" => "tab2"
    			)    		 		    		
    		),
    	"form" => array(
            "locking" => "true",
    		"name" => "{$this->object}",
    		"method" => "post",
    		"action" => "",
			"theme" => "default",
			"tab" => array(
				"tab1" => "Anagraphic Data",
				"tab2" => "Note"
				)
			)
		);
		
		
		
		// search form model
        $this->search_model = array(
        	"fields" => array(
        		"code" => array(
        			"label" => "Code",
        			"type" => "text",
        			"op" => "like",
        			"field" => "code"
        			)
        		),
        	
        	'form' => array(
        		'name' => $this->object.'_search'
        		)
    	);
		
		// 
		$this->list_model = array(
		"fields" => array(
    		"code" => array(
    			"header" => "Code",
    			"type" => "text"
    			),
    		"description" => array(
    			"header" => "Descr.",
    			"type" => "text"
    			),
    		"dob" => array(
    			"header" => "DOB",
    			"type" => "date"
    			),
            "centre" => array(
                "header" => "Centre",
                "type" => "select"
                ),
    		"registered" => array(
    			"header" => "Registration",
    			"type" => "date"
    			),
              "complete" => array(
    			"header" => "Reg. complete",
    			"type" => "select"
    			)
    		),
    	"grid" => array(
    		"op" => array(    			
    			"view" => array(
    				"link" => site_url("{$this->object}/xview/{$this->object}_id")
    			),
    			"id" => $this->object."_id"
    		),
    		"filter" => array(    			
    			"maxrows" => "all",
                     "yuimaxrows" => "50",
    			"default_sort" => "ORDER BY created DESC"
    		),
    		"table" => $this->object,
    		"base_url" => site_url($this->object."/xgrid"),
    		"theme" => "default",
    		"target" => "_parent"
    	)
		);
		
		$data = "";
		// create Form class models
		$this->myform->list_model = $this->list_model;
		$this->myform->search_model = $this->search_model;
		$this->myform->data_model = $this->data_model;
		$this->myform->form_model = $this->form_model;
		$this->myform->view_model = $this->view_model;
        $this->myform->customFunction &= $this->formCount($data);
		
		//$this->form->form_model["form"]["theme"] = $theme_form;
		
	}	

	function grid($pclass = "root", $pid = 0, $page = 1){
		
		if ($pclass > ""){
			$this->myform->list_model["filter"]["pclass"] = $pclass;
			$this->myform->list_model["filter"]["pid"] = $pid;
		}
		$search = $this->myform->generate_search();
		return $search."<br/><br/>".$this->myform->generate_grid($page);		
	
	}
	
	
	function view($id){
        
		$data["id"] = $id;
		$this->myform->view_model["fields"]["age"] =  array(
			"type" => "text",
			"label" => "Age",
			"tab" => "tab1"
                );
                  
		$this->myform->read_data($id);
		//calculating age
		if ($this->myform->form_data["dob"]>""){
			$now = date("m/d/Y");
			$dobit = explode("-", $this->myform->form_data["dob"]);
			$dob = $dobit[1]."/".$dobit[2]."/".$dobit[0];
			$diff = $this->myform->utils_dateDifference($dob, $now, "assoc_array");
			$this->myform->form_data["age"] = "{$diff["year"]} years, {$diff["month"]} months, {$diff["day"]} days";
		}
		return $this->myform->generate_view();
		
	}
    
    
    function age(){
         if ($this->myform->form_data["dob"]>""){
            $now = date("m/d/Y");
            $dobit = explode("-", $this->myform->form_data["dob"]);
            $dob = $dobit[1]."/".$dobit[2]."/".$dobit[0];
            $diff = $this->myform->utils_dateDifference($dob, $now, "assoc_array");
            return "{$diff["year"]} years, {$diff["month"]} months, {$diff["day"]} days";
        }
        else {return null;}
    }
    
    
    function title($id = null){
        
        $col = "code";
        if (!isset($this->myform->form_data[$col])){
            $this->myform->read_data($id);  
        }
        return $this->myform->form_data[$col];
        
    }

	
	function edit($id){
		
		//$this->myform->form_model["form"]["cancelUrl"] = site_url($this->object."/xview/$id");
        $this->myform->form_model["form"]["cancelUrl"] = site_url("admin/unlock/".$this->object."/$id");
		if ($this->myform->edit($id)){
			$this->relation->log("0", $this->object, $id, "edit", $this->cdata_user->get_user_id(), $this->myform->form_data["code"]);
			redirect($this->object."/xview/$id");
		}
		return $this->myform->generate_form();
	}

	
	function create($pclass = "root", $pid = "0"){    
		
		// add value to hidden field in the form
		$_REQUEST["pclass"] = $pclass;
		$_REQUEST["pid"] = $pid;
		$this->myform->form_model["form"]["cancelUrl"] = site_url($pclass."/xview/$pid");
		if (isset($_REQUEST['submit'])) {
			$this->myform->form_data = $_REQUEST;			
			if ($this->myform->form_check()){
				$this->myform->form_data['created'] = date("d-m-Y H:i:s");
				// save data
                $id = $this->myform->add_data();
                $this->relation->log("0", $this->object, $id, "add", $this->cdata_user->get_user_id(), $this->myform->form_data["code"]);
                //$this->createFolders($id, $pclass, $pid);
                redirect($this->object."/xview/$id");
			}
		}
        $this->myform->form_model["form"]["locking"] = "false"; 
		return $this->myform->generate_form();
	
	}
    
	
	function delete($id){

		list($pclass, $pid, $title) = $this->relation->getParentNode($this->object, $id);
		$this->relation->delete($this->object, $id);
		$this->relation->log("0", $this->object, $id, "del", $this->cdata_user->get_user_id(), $title);		
		redirect("$pclass/xview/$pid");
		
	}
	
    
	function createFolders($patientId, $pclass, $pid){
		
		// only for selected db
		if ($pclass == "db" AND $pid == 5){
		$CI =& get_instance();
        $CI->load->model('foldermodel');
        $this->folder =& $CI->foldermodel;
        $order = "0";
		$data["pclass"] = "patient";
		$data["pid"] = $patientId;
		$data["name"] = "Evaluation";
		$data["listorder"] = $order;
		$data["created"] = date("d-m-Y H:i:s");
		$this->folder->myform->form_data = $data;
		$this->folder->myform->add_data();
		$order = $order + 10;
		$data["pclass"] = "patient";
		$data["pid"] = $patientId;
		$data["name"] = "Family Members";
		$data["listorder"] = $order;
		$data["created"] = date("d-m-Y H:i:s");
		$this->folder->myform->form_data = $data;
		$this->folder->myform->add_data();
		$order = $order + 10;
		$data["pclass"] = "patient";
		$data["pid"] = $patientId;
		$data["name"] = "Clinical Course";
		$data["listorder"] = $order;
		$data["created"] = date("d-m-Y H:i:s");
		$this->folder->myform->form_data = $data;
		$this->folder->myform->add_data();
		$order = $order + 10;
		}
			
	}
    
    function formCount($data){
        return $data;
    }


}


?>